/*
 * Copyright 2009 Zero Separation
 *
 *     This file is part of PDSUtilities.
 *
 *  PDSUtilities is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  PDSUtilities is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with PDSUtilities.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

package com.zero_separation.pds.utilities;

import com.sun.sgs.app.AppContext;
import com.sun.sgs.app.ManagedObject;
import com.zero_separation.pds.utilities.indexingmaps.IndexableField;
import com.zero_separation.pds.utilities.indexingmaps.UniqueIndex;
import java.io.Serializable;
import java.math.BigInteger;

/**
 * This class provides an abstract base class to be used for building managed
 * objects onto. It automatically implements Serializable and ManagedObject and
 * when created it extracts a BigInteger unique identifier for this ManagedObject
 * from the Data Manager. That id is presented as an indexable field for use in
 * indexing maps and an index class is provided. This index class uses generics
 * to allow indexes on subclasses of AbstractManagedObject to specify what class
 * type is being indexed.
 *
 * <p>Equals and Hashcode are overridden to be based purely off this Id. Objects
 * will return as equals if they are the same object in the Data Manager.
 *
 * @author Tim Boura - Zero Separation
 */
public abstract class AbstractManagedObject implements Serializable, ManagedObject, HasId {

    /** The version of the serialized form of this class. */
    private static final long serialVersionUID = 1L;

    final IndexableField<BigInteger> dataManagerId = new IndexableField<BigInteger>(
            AppContext.getDataManager().getObjectId(this),
            AppContext.getDataManager().getObjectId(this));

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final AbstractManagedObject other = (AbstractManagedObject) obj;
        if (this.dataManagerId != other.dataManagerId && (this.dataManagerId == null || !this.dataManagerId.equals(other.dataManagerId))) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 37 * hash + (this.dataManagerId != null ? this.dataManagerId.hashCode() : 0);
        return hash;
    }

    /**
     * Returns a unique id number for this managed object in the database. The number
     * is guaranteed unique for all managed objects.
     */
    public BigInteger getId() {
        return dataManagerId.get();
    }

    /**
     * This class provides an IdIndex that can index any AbstractManagedObject
     * or class extending AbstractManagedObject from the contained Id. The index
     * can be specialised to contain a specific type of AbstractManagedObject by
     * specifying a type when creating the index.
     *
     * @param <T> The type of the object that extends AbstractManagedObject and is
     *  being indexed.
     */
    public static class IdIndex<T extends AbstractManagedObject> extends UniqueIndex<BigInteger, T> {

        @Override
        public IndexableField<BigInteger> getKey(T value) {
            return value.dataManagerId;
        }

    }

}
